/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import interfaces.BotObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import tools.StringTools;
import database.DBConnectionFactory;

/**
 * A class that represents a race
 * 
 * 
 */
public class Race implements BotObject {
   /**
    * Returns a Race info object for the specified race
    * 
    * @param race
    * @return a Race object. Returns null if no race is found
    */
   public static Race getRace(final String race) {
	  if (StringTools.isNullOrEmpty(race))
		 return null;
	  Race out = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM race WHERE name = ?");
		 ps.setString(1, race);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			out = new Race();
			out.setRace(rs.getString("name"));
			out.setOspecname(rs.getString("ospecname"));
			out.setOspecstr(rs.getInt("ospecstr"));
			out.setDspecname(rs.getString("dspecname"));
			out.setDspecstr(rs.getInt("dspecstr"));
			out.setElitename(rs.getString("elitename"));
			out.setEliteostr(rs.getInt("eliteostr"));
			out.setElitedstr(rs.getInt("elitedstr"));
			out.setElitesoutratio(rs.getInt("eliteoutratio"));
			out.setElitenw(rs.getDouble("elitenw"));
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   private String race = "";
   private String ospecname = "";
   private int ospecstr = 0;
   private String dspecname = "";
   private int dspecstr = 0;
   private String elitename = "";
   private int eliteostr = 0;
   private int elitedstr = 0;
   private int elitesoutratio = 0;

   private double elitenw = 0.0;

   public String getDspecname() {
	  return dspecname;
   }

   public int getDspecstr() {
	  return dspecstr;
   }

   public int getElitedstr() {
	  return elitedstr;
   }

   public String getElitename() {
	  return elitename;
   }

   public double getElitenw() {
	  return elitenw;
   }

   public int getEliteostr() {
	  return eliteostr;
   }

   public int getElitesoutratio() {
	  return elitesoutratio;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  return new LinkedList<String>();
   }

   public String getOspecname() {
	  return ospecname;
   }

   public int getOspecstr() {
	  return ospecstr;
   }

   public String getRace() {
	  return race;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  return new HashMap<String, Object>();
   }

   @Override
   public boolean saveToDB() {
	  return false;
   }

   public void setDspecname(final String dspecname) {
	  this.dspecname = dspecname;
   }

   public void setDspecstr(final int dspecstr) {
	  this.dspecstr = dspecstr;
   }

   public void setElitedstr(final int elitedstr) {
	  this.elitedstr = elitedstr;
   }

   public void setElitename(final String elitename) {
	  this.elitename = elitename;
   }

   public void setElitenw(final double elitenw) {
	  this.elitenw = elitenw;
   }

   public void setEliteostr(final int eliteostr) {
	  this.eliteostr = eliteostr;
   }

   public void setElitesoutratio(final int elitesoutratio) {
	  this.elitesoutratio = elitesoutratio;
   }

   public void setOspecname(final String ospecname) {
	  this.ospecname = ospecname;
   }

   public void setOspecstr(final int ospecstr) {
	  this.ospecstr = ospecstr;
   }

   public void setRace(final String race) {
	  this.race = race;
   }

}
